<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="小程序">
                <a-select placeholder="请选择" v-model="queryParam.id" @change="getList()">
                  <a-select-option :value="item.id" v-for="item of idList" :key="item.id">
                    {{ item.app_name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="行业类型">
                <a-select v-model="queryParam.category_id" allowClear showSearch :filterOption="filterOption">
                  <a-select-option :value="item.value" v-for="(item, index) of categoryList" :key="index">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="服务名称">
                <a-input placeholder="请输入服务名称" v-model="queryParam.service_name" allowClear></a-input>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div></div>

        <a-button type="primary" @click="$router.push({ name: 'serviceEdit' })" icon="plus">新建</a-button>
      </div>

      <a-table
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="record => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
      >
        <span slot="category_id" slot-scope="text, record">
          {{ record.category_id }}
        </span>

        <span slot="biz_status" slot-scope="text, record">
          <a-tooltip>
            <template slot="title" v-if="text.info && text.info.reject_reason">
              {{ text.info.reject_reason }}
            </template>
            <div class="name">
              <a-badge :color="getStatus(record.biz_status)[1]" :text="getStatus(record.biz_status)[0]" />
            </div>
          </a-tooltip>
        </span>

        <span slot="action" slot-scope="text, record">
          <!-- <a-button type="link" size="small" @click="handleItem('edit', record)">编辑</a-button>
          <a-divider type="vertical" /> -->
          <a-button
            :disabled="record.biz_status === 'AUDITING'"
            type="link"
            size="small"
            @click="handleItem('remove', record)"
            >删除</a-button
          >
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { serviceListQuery, getServiceCats, serviceDeleted } from '@/api/marketing/alipayPromotion'
import { alipayList } from '@/api/setting/applet'

const columns = [
  {
    title: 'ID',
    dataIndex: 'service_code',
    width: 200
  },
  {
    title: '服务名称',
    dataIndex: 'service_name'
  },
  {
    title: '类型',
    scopedSlots: {
      customRender: 'category_id'
    }
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'biz_status'
    }
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action'
    },
    width: 100
  }
]
export default {
  // components: {
  //   ContentDetails: () => import('@/components/tools/ContentDetail'),
  //   ModalImg: () => import('@/components/tools/ModalImg')
  // },

  data() {
    return {
      queryTime: [],
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,

      queryParam: {
        id: '',
        service_name: '',
        category_id: ''
      },
      list: [],

      item: null,

      categoryList: [],
      idList: []
    }
  },

  mounted() {
    this.getSelectList()
  },

  computed: {
    getStatus() {
      // EDITING(编辑中)
      // AUDITING(审核中)

      // PROMOTION(可推广)

      // STOP_PROMOTION(停止推广)
      // AUDIT_REJECT(审核驳回)
      // INVALID(失效)
      // PUNISHED(处罚)
      return status => {
        let text, color
        switch (status) {
          case 'EDITING':
            text = '编辑中'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case 'AUDITING':
            text = '审核中'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case 'PROMOTION':
            text = '可推广'
            color = '#65C817'
            break

          default:
            if (status === 'STOP_PROMOTION') {
              text = '停止推广'
            } else if (status === 'AUDIT_REJECT') {
              text = '审核驳回'
            } else if (status === 'INVALID') {
              text = '失效'
            } else if (status === 'PUNISHED') {
              text = '处罚'
            }
            color = '#FF4D4F'
            break
        }

        return [text, color]
      }
    }
  },

  methods: {
    // 将输入的内容与显示的内容进行匹配
    filterOption(value, option) {
      return option.componentOptions.children[0].text.indexOf(value) >= 0
    },

    getSelectList() {
      const hide = this.$message.loading('加载中', 0)
      Promise.all([getServiceCats(), alipayList()]).then(res => {
        hide()
        res.forEach((el, index) => {
          if (!el.code && el.data && el.data.length) {
            switch (index) {
              case 0:
                this.categoryList = el.data.map(el => {
                  return {
                    value: el[0],
                    name: el.splice(1, 3).join('/')
                  }
                })

                break

              case 1:
                this.idList = el.data
                this.queryParam.id = el.data[0].id
                this.getList()
                break
            }
          }
        })
      })
    },

    // 获取数据列表
    async getList(page_num = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize: page_size } = this

      const res = await serviceListQuery({
        ...queryParam,
        page_size: String(page_size),
        page_num: String(page_num)
      })

      if (res.code == 0) {
        let { total } = res.data
        this.total = total
        this.list = res.data.records || []
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handleItem(type, { service_code }) {
      switch (type) {
        case 'remove':
          serviceDeleted(service_code).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.getList()
              return
            }

            this.$message.error(res.msg)
          })
          break

        case 'edit':
          this.$router.push({ path: '/marketing/alipayPromotion/serviceEdit', query: { service_code } })
          break
      }
    }
  }
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}
</style>
